package vn.com.baseapplication;

import vn.com.baseapplication.constant.BaseConstant;
import vn.com.baseapplication.network.BaseNetwork;
import vn.com.baseapplication.network.NetworkHandlerCallback;
import vn.com.baseapplication.service.BaseBinder;
import vn.com.baseapplication.service.BaseReceiver;
import vn.com.baseapplication.service.BaseReceiverListener;
import vn.com.baseapplication.service.BaseService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;

public abstract class BaseActivity extends ActionBarActivity implements NetworkHandlerCallback,BaseReceiverListener{
	public static final String TAG = BaseActivity.class.getName();
	
	protected BaseNetwork network;
	
	private BaseService baseServices;
	private boolean servicesConnected = false;
	private BaseReceiver baseReceiver = new BaseReceiver(this);
	
	
	private ServiceConnection baseConnectionServices = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			servicesConnected = false;
			baseServices = null;
			
			notifyServicesStateChanged();
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			BaseBinder binder = (BaseBinder)service;
			baseServices = binder.getServices();
			servicesConnected = true;
			
			notifyServicesStateChanged();
			
			BaseActivity.this.onServiceConnected();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		network = new BaseNetwork(this);
		registerReceiver(baseReceiver, baseReceiver.getIntentFilter());
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ( keyCode == KeyEvent.KEYCODE_MENU ) {
	        // do nothing
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	protected void bindBaseService(Class<? extends BaseService> servicesClass) {
		Intent intentServices = new Intent(this, servicesClass);
		bindService(intentServices, baseConnectionServices, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(baseReceiver);
		if(servicesConnected)
			unbindService(baseConnectionServices);
	}
	
	@Override
	public void sendBaseBroadcast(Context context, Intent intent) {
		baseReceiver.sendBaseBroadcast(context, intent);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		
	}
	
	public void onServiceConnected(){
		if(!isServiceConnected()){
			try {
				throw new Exception("You must call bindBaseService method first.");
			} catch (Exception e) {
			}
		}
	}
	
	private void notifyServicesStateChanged(){
		Intent intentServicesConnected = new Intent();
		intentServicesConnected.putExtra(BaseConstant.MSG_FLAG, BaseConstant.MSG_SERVICES_STATE_CHANGE);
		intentServicesConnected.putExtra(BaseConstant.MSG_SERVICES_STATE_CHANGE, servicesConnected);
		intentServicesConnected.putExtra(BaseConstant.MSG_ACTIVITY_CURRENT_NAME, this.getClass().getName());
		Log.v(TAG, this.getClass().getName()+": service connected/disconnected.");
		BaseActivity.this.sendBaseBroadcast(BaseActivity.this, intentServicesConnected);
	}
	
	public BaseService getBaseServices(){
		return baseServices;
	}
	
	public boolean isServiceConnected(){
		return servicesConnected;
	}

	@Override
	public void onNetLoading(int requestId) {
		
	}
	
	@Override
	public void onNetSuccess(int requestId, String response) {
		
	}
	
	@Override
	public void onNetSuccessCache(int requestId, String response) {
		
	}
	
	@Override
	public void onNetFailed(int requestId, String error) {
		
	}
	
	@Override
	public void onNetTimeOut(int requestId, Throwable error) {
		
	}
}
